package yxy.game.pm2.command.temple;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.heroadvanced;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.TempleMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.hero.FormatHandle;
import yxy.game.pm2.module.hero.HeroHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;

/****************************************
 * @name CMD_SevenStarRestoreInfo
 * @description 置换神殿 七星祭坛武将回退信息
 * @author 梁健彬
 * @since 2022-05-20
 ***************************************/
@MessageType(proto = Command.SevenStarRestoreInfo, classtype = TempleMessage.SevenStarRestoreInfoRequest.class)
public class CMD_SevenStarRestoreInfo extends Command<TempleMessage.SevenStarRestoreInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, TempleMessage.SevenStarRestoreInfoRequest request) {
        String playerCode = messageSource.getTag();
        String heroCode = request.getHero();

        TempleMessage.SevenStarRestoreInfoResponse.Builder response = TempleMessage.SevenStarRestoreInfoResponse.newBuilder();

        HeroHandle heroHandle = new HeroHandle();

        Hero hero = heroHandle.getHero(playerCode, heroCode);

        if (hero == null) {
            send(messageSource, response, Result.HeroNotExists);//武将不存在
        }

        heroadvanced _heroadvanced = DataCenter.getData(hero.advancedCode(), heroadvanced.class);

        if (_heroadvanced.getBackoff() <= 0) {
            send(messageSource, response, Result.TempleHeroCantRestore);// 表中不可回退该武将
        }

        if (hero.isLock() || new FormatHandle().heroInFormat(playerCode, hero.getCode())) {
            send(messageSource, response, Result.TempleHeroCantRestore);// 上锁或上阵的武将不可回退
        }

        if (hero.getStar() > 9 || hero.getStar() < 6) {
            send(messageSource, response, Result.TempleHeroCantRestore);// 9星以上，6星一下的武将不能回退
        }

        //将7，8，9星的武将，自动转成本体碎片和同阵容碎片
        Collection<Currency> currencies = heroHandle.getSevenStarRestoreInfo(hero);

        if (currencies.size() == 0) {
            send(messageSource, response, Result.DataError);// 参数错误
        }

        ArrayList<Currency> returnList = new ArrayList<>();
        for (Currency currency : currencies) {
            if (currency.getDataId() == 8015) {
                continue;
            }
            returnList.add(currency);
        }

        //排序
        Collections.sort(returnList, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                Currency currency1 = (Currency) o1;
                Currency currency2 = (Currency) o2;
                return currency2.getDataId() - currency1.getDataId();
            }
        });

        response.addAllBack(new CurrencyHandle().packItems(returnList));

        send(messageSource, response, Result.Success);
    }

    private void send(MessageSource messageSource, TempleMessage.SevenStarRestoreInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


    //全阵营的不返回给前端，即不显示


}